package com.exporter.listeners;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import com.exporter.exception.ExporterException;
import com.exporter.utility.ExporterUtility;

public class SourceFolderDcumentListener implements DocumentListener
{
    JCheckBox chkbxEnableAdvOptions;
    JFrame frame;

    public SourceFolderDcumentListener(JCheckBox chkbxEnableAdvOptions, JFrame frame)
    {
        this.chkbxEnableAdvOptions = chkbxEnableAdvOptions;
        this.frame = frame;
    }

    @Override
    public void changedUpdate(DocumentEvent de)
    {
    }

    @Override
    public void insertUpdate(DocumentEvent de)
    {
        updateionMethod(de);
    }

    @Override
    public void removeUpdate(DocumentEvent de)
    {
    }
   
    private void updateionMethod(DocumentEvent de)
    {
        try
        {
            String path = de.getDocument().getText( 0, de.getDocument().getLength() );
            boolean valid = false;
            try
            {
                valid = ExporterUtility.validateWorkspace( path );
            }
            catch ( ExporterException eex )
            {
                JOptionPane.showMessageDialog( frame, eex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
                return;
            }
            finally
            {
                if ( valid )
                {
                    chkbxEnableAdvOptions.setEnabled( true );
                }
                else
                {
                    chkbxEnableAdvOptions.setEnabled( false );
                }
            }
        }
        catch ( BadLocationException eex )
        {
            JOptionPane.showMessageDialog( frame, eex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
            return;
        }
    }

}
